package com.marchsoft.musicalstore.service;

import com.marchsoft.musicalstore.entity.OrderMy;
import com.marchsoft.musicalstore.entity.Orders;
import com.marchsoft.musicalstore.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class OrderSer {
    @Autowired
    OrderRepository orderRepository;

    public List<Map<String, Object>> showShopCart(Integer id, Integer state){
        if(state==0){
            return orderRepository.getByUserId(id);
        }
        else{
            List<Map<String, Object>> allByUserIdAndIsOut = orderRepository.getAllByUserIdAndIsOut(id, state);
            return allByUserIdAndIsOut;
        }
    }

    public OrderMy getProductNum(Integer id,Integer isAdd){
        OrderMy product = orderRepository.findById(id).get();
        OrderMy save = product;
        if(product.getBuyType()==1){
            int number = product.getProductNum();
            if(number+isAdd>0){
                product.setProductNum(number+isAdd);
                save = orderRepository.save(product);
            }
        }
        return save;
    }
}
